#include "stdafx.h"
#include "Drawing.h"


Drawing::~Drawing()
	{
	ShapeNode *nodehelp;
	ShapeNode *node = _first;
		while (node)
			{
			nodehelp = node->_next;
			delete node;
			node = nodehelp;
		}
	}

Drawing::ShapeNode* Drawing::getLastNode()
	{
	ShapeNode *node = 0;
	if (_first)
		for (node= _first; node->_next != 0; node = node->_next);
	return node;
	}

void Drawing::add(Shape &sh)
	{
	ShapeNode *lastNode = getLastNode(), *newNode = new ShapeNode(sh);
	if (lastNode)
		lastNode->_next = newNode;
	else
		_first = newNode;
	}

Rectangle Drawing::getContour()
	{
	Rectangle result = Shape::getContour();
	for(ShapeNode *node = _first; node != 0; node = node->_next)
		{
		Rectangle nodeContour = node->_shape.getContour();
		result.columns = MAX(result.columns, nodeContour.column+nodeContour.columns);
		result.rows = MAX(result.rows, nodeContour.row+nodeContour.rows);
		}
	return result;
	}

char Drawing::charAt(int w, int h)
	{
	char result = ' ';
	for(ShapeNode *node = _first; result == ' ' && node != 0; node = node->_next)
		{
		Shape &shape = node->_shape;
		Rectangle shapeContour = shape.getContour();
		if (shapeContour.contains(w, h))
			result = shape.charAt(w-shapeContour.column, h-shapeContour.row);
		}
	return result;
	}

